import 'package:flutter/material.dart';
import 'package:ls_work/a_training/model/train_entity.dart';
import 'package:ls_work/a_training/widgets/training_cell.dart';
import 'package:ls_work/mvp/svp_progress.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/toast.dart';
import 'package:ls_work/widgets/my_refresh_divider_list.dart';
import 'dart:convert';
import 'package:dio/dio.dart';

class TrainingMorePage extends StatefulWidget {
  final String? trainingType;
  final String? trainingTypeName;
  TrainingMorePage({Key? key, this.trainingType, this.trainingTypeName});
  @override
  _TrainingMorePageState createState() => _TrainingMorePageState();
}

class _TrainingMorePageState extends State<TrainingMorePage> {
  bool isMore = false;
  int _page = 1;
  List<TrainInfo> dataList = [];
  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _fectData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.trainingTypeName??""),
        elevation: 0.5,
      ),
      body: DeerSeparatedListView(
        key: Key('_list'),
        itemCount: dataList.length,
        onRefresh: _onRefresh,
        loadMore: _loadMore,
        hasMore: isMore,
        itemBuilder: (_, index) {
          return Container(
            padding: EdgeInsets.fromLTRB(0, 12, 0, 12),
            child: TrainingCell(
              info: dataList[index],
            ),
          );
        },
        separatorBuilder: (_, int index) {
          return Container(
            width: double.infinity,
            height: 1,
            color: Colours.bg_color,
          );
        },
      ),
    );
  }

  _fectData() {
    SVProgressHUD page = SVProgressHUD(context);
    var map = {
      'page': _page,
      'rows': 20,
      'type': widget.trainingType,
    };
    page.showProgress();
    HttpUtil.instance.getDio().post(HttpApi.groupBeanList, data: map).then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          //
          if (map['data'] != null) {
            TrainListEntity model = TrainListEntity.fromJson(map['data']);
            if ((model.rows??[]).length > 0) {
              isMore = (model.rows??[]).length == 20;
              setState(() {
                if (_page == 1) {
                  /// 刷新
                  dataList.clear();
                  dataList.addAll(model.rows??[]);
                } else {
                  dataList.addAll(model.rows??[]);
                }
              });
            }
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }

  Future _onRefresh() async {
    _page = 1;
    _fectData();
  }

  Future _loadMore() async {
    _page++;
    _fectData();
  }
}
